<?php

namespace component\system\system_message;

use common\model\Model;

/**
 * Class SystemMessageModel
 * @package component\system\system_message
 * @property integer $id
 * @property integer $type
 * @property string $user_id
 * @property string $title
 * @property string $message
 * @property integer $read_time
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $delete_time
 */
class SystemMessageModel extends Model
{
    protected $table = 'system_message';

    /**
     * @var array
     */
    protected $schema = [
        'id' => 'integer',
        'type' => 'integer',
        'user_id' => 'string',
        'title' => 'string',
        'message' => 'string',
        'read_time' => 'integer',
        'create_time' => 'integer',
        'update_time' => 'integer',
        'delete_time' => 'integer',
    ];

    /**
     * @param SystemMessageModel $params
     */
    public static function onBeforeInsert($params)
    {
        if (!$params->type) {
            $params->setAttr('type',1);
        }
    }

    /**
     * @param $value
     * @return int
     */
    protected function setTypeAttr($value)
    {
        $value = intval($value);

        return $value ? $value : 1;
    }

    /**
     * @param $value
     * @return string
     */
    protected function setRoleIdAttr($value)
    {
        if (is_array($value) && $value) {
            $value = implode(',', $value);
        } else {
            $value = '';
        }

        return $value;
    }

    /**
     * @param $value
     * @return array|string
     */
    protected function getRoleIdAttr($value)
    {
        if ($value) {
            $value = explode(',', $value);
        } else {
            $value = '';
        }

        return $value;
    }
}    